const app = angular.module("myApp",['ui.bootstrap', 'ui.bootstrap.treeview']);

app.controller("manageController2",['$scope','$compile','$sce',function($scope,$compile,$sce){
    $scope.query={
        fault:FaultNode.faults[0]
    };

    $scope.toJSON = function (val) {
        return angular.toJson(val,true);
    };

    // $scope.faults=FaultNode.faults;
    FaultNode.loadFaults.callbacks.push(()=>$scope.faults=FaultNode.faults);
    FaultNode.loadFaults.callbacks.push(()=>$scope.query.fault=FaultNode.faults[0]);
    FaultNode.loadFaults();

    $scope.components=ComponentNode.components;
    ComponentNode.loadComponents.callbacks.push(()=>$scope.components=ComponentNode.components);
    ComponentNode.loadComponents();

    $scope.graphTree = new TreeView();
    $scope.$watch('query',function () {
        console.info($scope.query);
        if($scope.query.fault)
            Graph.loadGraph($scope.query.fault.id,function (g) {
                $scope.graph = g;
                $scope.graphTree.nodes = [$scope.graph.toJsonTree()];
                $scope.graphTree.collapseAll();
                console.info($scope.graphTree);

                $scope.$apply();
                let queryUrl = `start n=node(${$scope.query.fault.id}) match (n)-[*]->(p) return n,p`;
                $scope.faultUrl = $sce.trustAsResourceUrl(`../browser/NeoEmbed.html?query=${encodeURIComponent(queryUrl).replace(/=/g,'%3d')}`);
            });
    },true);

    $scope.$watch('graphTree.selectedNode',function () {
        let data = $scope.graphTree.selectedNode;
        if($scope.graph){
            let template = $scope.graph.getNode(data.referId).getReadonlyTemplate(data.referEdge,$scope.graph);
            let tp = $compile( template )( $scope );
            angular.element("#nodeDetail2").html("").append( tp );
        }
    },true);

    $scope.displayAllTree = function () {
        let gs = [];
        let gts =  FaultNode.faults.length;
        let gtn = 0;
        for (let f of FaultNode.faults){
            Graph.loadGraph(f.id,(g)=>{
                gs.push(g);
                gtn ++;
                if(gtn==gts){
                    $scope.graphTree.nodes = gs.map(g=>g.toJsonTree());
                    $scope.graphTree.collapseAll();
                }
            });
        }
    };

    $scope.allowFeature=(index) => false;

    $scope.allowCause=(index) => false;

    $scope.allowComponent=(index) => false;

    $scope.attachNode=(type,id)=>null;

    $scope.attachFeature=(id)=>$scope.attachNode('Feature',id);
    $scope.attachReason = (id)=>$scope.attachNode('Reason',id);
    $scope.attachComponent = function (index) {
        let cp = ComponentNode.components
            .filter(nd=>{
                for (let eg of $scope.graph.edges){
                    if(eg.from==index&&eg.to==nd.id)
                        return false;
                }
                return true;
            })
            [0];
        let edge = new Edge($scope.graph,index,cp.id);
        $scope.graph.addNode(cp);
        $scope.graph.edges.push(edge);
    };

    $scope.attachGroup = (index,groupType)=> $scope.attachNode(groupType,index);

    $scope.appendArgv = function (index) {
        $scope.graph.getNode(index).params.push(0);
    };

    $scope.removeArgv = function (index,position) {
        $scope.graph.getNode(index).params.splice(position,1);
    };

    $scope.removeEdge = edge=>null;
}]);
